﻿using CTBU.DAO;
using CTBU.Entity;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;


namespace Student.Controllers
{
    public class HomeController : Controller
    {
   
        private StudentDAO _stuDao= new StudentDAO();
      

        public ActionResult Login()
        {
            return View();
        }







        #region 学生管理
        public ActionResult Index()
        {
            if (Session["UserID"] == null)
                return RedirectToAction("Login");
            else
            {
                IList<TbStudent> myList = new StudentDAO().GetAll();


                ViewBag.myList = myList;

                return View();

            }



        }



        #endregion 学生管理


        #region 课程管理

        private CourseDAO _courseDao = new CourseDAO();

        public ActionResult Course()
        {


            return View();

        }

       


        //AddOrUpdateUser
        public JsonResult AddOrUpdateCourse(int Id, string SN, string SCName)
        {
            if (Id <= 0)
            {
                //新增


                TbCourse newItem = new TbCourse();
                newItem.SN = SN;
                newItem.SCName = SCName;


                _courseDao.Insert(newItem);

                return Json(new { ID = newItem.Id, Msg = "success" }, JsonRequestBehavior.AllowGet);

            }
            else
            {
                //更新
                TbCourse oldItem = _courseDao.GetByID(Id);
                if (oldItem != null)
                {
                    oldItem.SN = SN;
                    oldItem.SCName = SCName;
                    _courseDao.Update(oldItem);
                    return Json(new { ID = oldItem.Id, Msg = "success" }, JsonRequestBehavior.AllowGet);
                }
                else
                {
                    return Json(new { ID = -1, Msg = "读取课程出错，找不到课程！" }, JsonRequestBehavior.AllowGet);
                }


            }



        }



        public JsonResult getCourses()
        {




            IList<TbCourse> myList = _courseDao.GetAll();

            return Json(myList, JsonRequestBehavior.AllowGet);


        }


        //DelUser
        public JsonResult DelCourse(int Id)
        {

            try
            {
                _courseDao.Del(Id);


                return Json(new { ID = 1, Msg = "Success" }, JsonRequestBehavior.AllowGet);
            }
            catch (Exception ee)
            {

                return Json(new { ID = -1, Msg = ee.Message }, JsonRequestBehavior.AllowGet);
            }



        }


        #endregion 课程管理



        #region 课程管理

        private ScoreDAO _scoreDao = new ScoreDAO();
        private VwScoreDAO _vwScoreDao = new VwScoreDAO();

        public ActionResult Score()
        {

            IList<TbStudent> allStudent = _stuDao.GetAll();
            IList<TbCourse> allCourse = _courseDao.GetAll();

            ViewBag.allStudent = allStudent;
            ViewBag.allCourse = allCourse;

            return View();

        }




        //AddOrUpdateUser
        public JsonResult AddOrUpdateScore(int Id, int StuID, int CourseID,decimal score)
        {
            if (Id <= 0)
            {
                //新增


                TbScore newItem = new TbScore();
                newItem.ICoureN=CourseID;
                newItem.IStudentN = StuID;
                newItem.FScore = score;


                _scoreDao.Insert(newItem);

                return Json(new { ID = newItem.Id, Msg = "success" }, JsonRequestBehavior.AllowGet);

            }
            else
            {
                //更新
                TbScore oldItem = _scoreDao.GetByID(Id);
                if (oldItem != null)
                {
                    oldItem.ICoureN = CourseID;
                    oldItem.IStudentN = StuID;
                    oldItem.FScore = score;
                    _scoreDao.Update(oldItem);
                    return Json(new { ID = oldItem.Id, Msg = "success" }, JsonRequestBehavior.AllowGet);
                }
                else
                {
                    return Json(new { ID = -1, Msg = "读取成绩出错，找不到成绩！" }, JsonRequestBehavior.AllowGet);
                }


            }



        }



        public JsonResult getScore()
        {




            IList<VwScore> myList = _vwScoreDao.GetAll();

            return Json(myList, JsonRequestBehavior.AllowGet);


        }


        //DelUser
        public JsonResult DelScore(int Id)
        {

            try
            {
                _scoreDao.Del(Id);


                return Json(new { ID = 1, Msg = "Success" }, JsonRequestBehavior.AllowGet);
            }
            catch (Exception ee)
            {

                return Json(new { ID = -1, Msg = ee.Message }, JsonRequestBehavior.AllowGet);
            }



        }


        #endregion 课程管理


        #region 测试页面

        public ActionResult TestArea()
        {
            return View();
        }


        #endregion 测试页面
    }
}
